package com.eportal.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.eportal.action.background.TAgentAction;
import com.eportal.domain.Sysopt;
import com.eportal.vo.controller.background.tagent.ReportVO;
import com.eportal.service.TAgentService;
import com.eportal.vo.controller.background.tagent.TAgentSelectListVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 合作方管理
 * Created by Saber on 2017/8/2.
 */
@Controller
@RequestMapping("/admin/tagent")
public class TAgentController {

    private static Logger logger = LoggerFactory.getLogger(TAgentController.class.getName());

    @Resource
    private TAgentService tAgentService;


    /**
     * 合作方列表（下拉）
     * @author Saber
     * @Date 2017/8/2 下午12:53
     * @param request
     * @see TAgentAction#list()
     * @return
     */
    @RequestMapping(value = "selectList.json")
    @ResponseBody
    public JSONObject selectList(HttpServletRequest request){

        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {

            List<TAgentSelectListVO> list = tAgentService.selectList(user,userRole);
            json.put("code",1);
            json.put("msg","ok");
            json.put("data",list);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取合作方列表（下拉）异常:",e);
            json.put("code",0);
            json.put("msg","服务器异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 数据统计
     * @author Saber
     * @Date 2017/8/22 上午9:00
     * @param request
     * @param agtCode           公司
     * @param communityId       小区
     * @see TAgentAction#pmsReports()
     * @return
     */
    @RequestMapping(value = "pmsReports.json")
    @ResponseBody
    public JSONObject pmsReports(HttpServletRequest request,String agtCode,String communityId ){
        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            List<ReportVO> dataList = null;
            if(StringUtils.isBlank(agtCode)){//全部
                dataList = tAgentService.getPmsReports(user,userRole);
            }else if(StringUtils.isBlank(communityId)){//合作方
                dataList = tAgentService.tagentReports2(agtCode);
            }else{//社区报表
                dataList = tAgentService.tcomuReports2(agtCode,communityId);
            }

            if(dataList.size()==0){
                json.put("msg","暂无数据");
            }else{
                json.put("msg","ok");
            }
            json.put("data",dataList);
            json.put("code",1);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("门禁管理列表异常:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }

    }

}
